﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;

namespace An_Im.Model.QuickHelper
{

    public enum MouseButton
    {
        Left, Middle, Right
    }

    /// <summary>
    /// 鼠标管理。
    /// </summary>
    static class CursorHelper
    {
        [DllImport("user32.dll")]
        static extern bool SetCursorPos(int X, int Y);
        [DllImport("user32.dll")]
        static extern void mouse_event(MouseEventFlag flags, int dx, int dy, uint data, UIntPtr extraInfo);
        [Flags]
        enum MouseEventFlag : uint
        {
            Move = 0x0001,
            LeftDown = 0x0002,
            LeftUp = 0x0004,
            RightDown = 0x0008,
            RightUp = 0x0010,
            MiddleDown = 0x0020,
            MiddleUp = 0x0040,
            XDown = 0x0080,
            XUp = 0x0100,
            Wheel = 0x0800,
            VirtualDesk = 0x4000,
            Absolute = 0x8000
        }


     

        /// <summary>
        /// 绝对移动。
        /// </summary>
        /// <param name="x"></param>
        /// <param name="y"></param>
        public static void SetCursorPositionAbs(int x, int y)
        {
            mouse_event(MouseEventFlag.Move | MouseEventFlag.Absolute, x, y, 0, UIntPtr.Zero);
        }


        /// <summary>
        /// 相对移动。
        /// </summary>
        /// <param name="x"></param>
        /// <param name="y"></param>
        public static void SetCursorPositionRel(int dx, int dy)
        {
            mouse_event(MouseEventFlag.Move, dx, dy, 0, UIntPtr.Zero);
        }

        /// <summary>
        /// 鼠标按下。
        /// </summary>
        /// <param name="mouseButton"></param>
        public static void Press(MouseButton mouseButton)
        {
            switch (mouseButton)
            {
                case MouseButton.Left:
                    mouse_event(MouseEventFlag.LeftDown, 0, 0, 0, UIntPtr.Zero);
                    break;
                case MouseButton.Middle:
                    mouse_event(MouseEventFlag.MiddleDown, 0, 0, 0, UIntPtr.Zero);
                    break;
                case MouseButton.Right:
                    mouse_event(MouseEventFlag.RightDown, 0, 0, 0, UIntPtr.Zero);
                    break;
                default:
                    break;
            }

        }


        /// <summary>
        /// 鼠标释放。
        /// </summary>
        /// <param name="mouseButton"></param>
        public static void Release(MouseButton mouseButton)
        {
            switch (mouseButton)
            {
                case MouseButton.Left:
                    mouse_event(MouseEventFlag.LeftUp, 0, 0, 0, UIntPtr.Zero);
                    break;
                case MouseButton.Middle:
                    mouse_event(MouseEventFlag.MiddleUp, 0, 0, 0, UIntPtr.Zero);
                    break;
                case MouseButton.Right:
                    mouse_event(MouseEventFlag.RightUp, 0, 0, 0, UIntPtr.Zero);
                    break;
                default:
                    break;
            }

        }

        /// <summary>
        /// 鼠标点击
        /// </summary>
        /// <param name="mouseButton"></param>
        public static void Click(MouseButton mouseButton)
        {
            switch (mouseButton)
            {
                case MouseButton.Left:
                    mouse_event(MouseEventFlag.LeftDown | MouseEventFlag.LeftUp, 0, 0, 0, UIntPtr.Zero);
                    break;
                case MouseButton.Middle:
                    mouse_event(MouseEventFlag.MiddleDown | MouseEventFlag.MiddleUp, 0, 0, 0, UIntPtr.Zero);
                    break;
                case MouseButton.Right:
                    mouse_event(MouseEventFlag.RightDown | MouseEventFlag.RightUp, 0, 0, 0, UIntPtr.Zero);
                    break;
                default:
                    break;
            }
        }


    }
}
